package org.example.utils;

import java.io.File;

/**
 * 表格工具类
 */
public class TableStrUtil {
    /**
     * 将表名转换类目
     *
     * @param str
     * @return
     */
    public static StringBuilder getClassName(String str) {
        StringBuilder sb = new StringBuilder();
        char[] c = str.toCharArray();

        //表名转换成entity名
        for (int i = 0; i < c.length; i++) {
            if (i == 0) {
                sb.append((char) (c[i] - 32));
            } else {
                if ('_' == c[i]) {
                    c[i + 1] = (char) (c[i + 1] - 32);
                    continue;
                } else {
                    sb.append(c[i]);
                }
            }
        }
        return sb;
    }

    /**
     * 获取变量名
     *
     * @param str
     * @return
     */
    public static StringBuilder getVarName(String str) {
        StringBuilder sb = new StringBuilder();
        char[] c = str.toCharArray();

        for (int i = 0; i < c.length; i++) {
            if ('_' == c[i]) {
                c[i + 1] = (char) (c[i + 1] - 32);
                continue;
            } else {
                sb.append(c[i]);
            }

        }
        return sb;
    }

    /**
     * 去掉tb前缀
     *
     * @return
     */
    public static String delPrefix(String str) {
        int indexOf = str.indexOf("tb_");
        if (indexOf == 0) {
            str = str.substring(3, str.length());
        }
        return str;
    }

    /**
     * 地址变量名称
     *
     * @param str
     * @return
     */
    public static String getUrlVarName(String str) {
        return delPrefix(str).toUpperCase();
    }

    /**
     * 地址变量名称
     *
     * @param str
     * @return
     */
    public static String getUrl(String str) {
        return delPrefix(str).replaceAll("_", "/");
    }

    /**
     * 将包名转换为路径名
     *
     * @param packagePath
     * @return
     */
    public static String packageToFile(String packagePath) {
        StringBuilder builder = new StringBuilder();
        String[] split = packagePath.split("\\.");
        for (String str : split) {
            builder.append(File.separator + str);
        }
        return builder.toString();
    }
}
